<?php
/**
 * ZView
 *
 * Zend Frameworks View Component for TYPO3
 *
 * @package    ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */

/** Abstract class for extension */
require_once 'Zend/View/Helper/Abstract.php';

/**
 * Helper to generate a "popup" link element
 *
 * @package ZView
 * @copyright  Copyright (c) Zephir AG, www.zephir.ch
 */
class Tx_Zview_Helper_T3popup extends Zend_View_Helper_Abstract
{

	/**
	 * The TYPO3 plugin
	 *
	 * @var tslib_pibase
	 */
	private $_pi;

	/**
	 * Generates a popup link adding a js onclick (acts as a wrapper)
	 *
	 * @param string $str
	 * @param string $winName
	 * @param array $winParams
	 * @return string The wrapped html link with the popup js
	 */
	public function t3popup($str, $winName='', array $winParams=array())
	{
		// default values
		$winParams['width'] = isset($winParams['width']) ? $winParams['width'] : 670;
		$winParams['height'] = isset($winParams['height']) ? $winParams['height'] : 500;
		$winParams['status'] = isset($winParams['status']) ? $winParams['status'] : 0;
		$winParams['menubar'] = isset($winParams['menubar']) ? $winParams['menubar'] : 0;
		$winParams['scrollbars'] = isset($winParams['scrollbars']) ? $winParams['scrollbars'] : 0;
		$winParams['resizable'] = isset($winParams['resizable']) ? $winParams['resizable'] : 0;

		$winParamsStr = '';
		$delim = '';
		foreach($winParams AS $key=>$val) {
			$winParamsStr .= sprintf('%s%s=%s', $delim, $key, $val);
			$delim = ',';
		}
		return $this->_pi->pi_openAtagHrefInJSwindow($str, $winName, $winParamsStr);
	}

    /**
     * Set the View object
     *
     * @param  Zend_View_Interface $view
     * @return Zend_View_Helper_Abstract
     */
	public function setView(Zend_View_Interface $view)
	{
		$this->view = $view;
		$this->_pi = $view->getPi();
		if(empty($this->_pi) || (!$this->_pi instanceof tslib_pibase)) {
			throw new Exception('Please set tslib_pibase using $view->setPi($this) in your plugin class.');
		}
        return $this;
	}

}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3popup.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zview/helper/T3popup.php']);
}
